package com.traykai.web.meeting.demo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.traykai.framework.util.JsonUtils;
import com.traykai.web.meeting.demo.services.DemoService;
import com.traykai.web.meeting.demo.services.TestService;
import com.traykai.web.meeting.demo.util.DateUtil;
import com.traykai.web.meeting.demo.util.EscapeUtil;
import com.traykai.web.meeting.demo.vo.ArticleVo;
import com.traykai.web.meeting.demo.vo.Comment2Vo;
import com.traykai.web.meeting.demo.vo.CommentVo;
import com.traykai.web.meeting.demo.vo.DemoVO;
import com.traykai.web.meeting.demo.vo.TypeVo;

/**
 * 文章评论业务处理Controller层
 * DemoController
 *
 * @author lengqx
 * @date 2016年6月15日下午2:28:30
 */
@RequestMapping("demo")
@Controller
public class DemoController {
	
	@Autowired
	private DemoService service;
	
	@Autowired
	private TestService testService;
	/**
	 * 所有文章列表
	 * articleList
	 *
	 * @param model
	 * @return
	 */
	@RequestMapping("index")
	public String articleList(Model model){
		List<TypeVo> typeVos=service.queryTypes();
		model.addAttribute("typeVos",typeVos);
		List<ArticleVo> articleVos=service.queryArticles();
		model.addAttribute("articleVos",articleVos);
		System.out.println(testService.toString());
		return "demo/demo";
	}
	
	/**
	 * 查询文章内容
	 * queryArticleById
	 *
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("queryArticleById")
	public String queryArticleById(int id,Model model){
		ArticleVo articleVo=service.selectArticleById(id);
		model.addAttribute("articleVo",articleVo);
		List<CommentVo> commentVos=service.selectCommentsByArticleId(id);
		model.addAttribute("commentVos", commentVos);
		return "demo/content";
	}
	
	/**
	 * 所有文章类型
	 * queryTypes
	 *
	 * @return
	 */
	@RequestMapping("types")
	@ResponseBody
	public String queryTypes(){
		List<TypeVo> typeVo=service.queryTypes();
		return JsonUtils.toJSON(typeVo);
	}
	
	/**
	 * 查询不同文章类型的文章列表
	 * queryArticlesByTypeId
	 *
	 * @param typeId
	 * @param model
	 * @return
	 */
	@RequestMapping("queryArticlesByTypeId")
	public String queryArticlesByTypeId(int typeId,Model model){
		TypeVo typeVo=service.queryTypeById(typeId);
		model.addAttribute("typeVo",typeVo);
		List<ArticleVo> articleVos=service.queryArticlesByTypeId(typeId);
		model.addAttribute("articleVos", articleVos);
		return "demo/article";
	}
	
	/**
	 * 添加文章
	 * addArticle
	 *
	 * @param vo
	 * @return
	 */
	@RequestMapping("addArticle")
	@ResponseBody
	public DemoVO addArticle(ArticleVo vo){
		try {
			vo.setPublishTime(DateUtil.getToDay());
			vo.setCommentNum(0);
			if(vo!=null){
				String content=vo.getContent();
				vo.setContent(EscapeUtil.escapeHtmlOFLtAndGt(content));			
			}
			service.saveArticle(vo);
			return new DemoVO("200","添加文章成功");
		} catch (Exception e) {
			return new DemoVO("500", "添加文章失败");
		}	
	}

	/**
	 * 添加评论
	 * addComment
	 *
	 * @param vo
	 * @return
	 */
	@RequestMapping("addComment")
	@ResponseBody
	public DemoVO addComment(CommentVo vo){
		vo.setCreateTime(DateUtil.getToDay());
		try {
			if(vo!=null){
				String content=vo.getContent();
				vo.setContent(EscapeUtil.escapeHtmlOFLtAndGt(content));			
			}
			service.saveComment(vo);
			service.updateCommentNum(vo.getArticleId());
			return new DemoVO("200", JsonUtils.toJSON(vo));
		} catch (Exception e) {
			e.printStackTrace();
			return new DemoVO("500", "评论失败");
		}
	}
	
	/**
	 * 追加评论
	 * addToComment
	 *
	 * @param content
	 * @param articleId
	 * @param commentId
	 * @return
	 */
	@RequestMapping("addToComment")
	@ResponseBody
	public DemoVO addToComment(String content,int articleId,int commentId){
		CommentVo vo=new CommentVo();
		try {
			vo.setCreateTime(DateUtil.getToDay());
			CommentVo vo2=service.selectCommentVoById(commentId);
			StringBuffer sb=new StringBuffer();
			sb.append("<div class='onther-comment'>")
			  .append("<h4>引用来自'")
			  .append(vo2.getId())
			  .append("'的评论,发表时间<span>")
			  .append(vo2.getCreateTime())
			  .append("</span></h4>")
			  .append("<p>")
			  .append(vo2.getContent())
			  .append("</p></div>")
			  .append(content);  
          	vo.setContent(sb.toString());
          	vo.setArticleId(articleId);
			service.saveComment(vo);
			service.updateCommentNum(articleId);
			return new DemoVO("200",JsonUtils.toJSON(vo));
		} catch (Exception e) {
			// TODO: handle exception
			return new DemoVO("500", "评论失败");
		}
	}
	
//	第二种评论处理方式
	@RequestMapping("selectArticleById")
	public String selectArticleById(int id,Model model){
		ArticleVo articleVo=service.selectArticleById(id);
		model.addAttribute("articleVo",articleVo);
		List<Comment2Vo> vos=service.selectCommentCopysByArticleId(id);
		List<Comment2Vo> vo2s=new ArrayList<>();
		for (Comment2Vo comment2Vo : vos) {
			if(comment2Vo!=null){
				List<Comment2Vo> chilrenVos=service.selectCommentCopyChildrenByParentId(comment2Vo.getId());
				comment2Vo.setComments(chilrenVos);
				vo2s.add(comment2Vo);
			}
		}
		model.addAttribute("commentVos", vo2s);
		return "demo/content2";
	}
	
	@RequestMapping("addCommentCopy")
	@ResponseBody
	public DemoVO addCommentCopy(Comment2Vo vo){
		vo.setCreateTime(DateUtil.getToDay());
		try {
			if(vo!=null){
				String content=vo.getContent();
				vo.setContent(EscapeUtil.escapeHtmlOFLtAndGt(content));			
			}
			service.saveCommentCopy(vo);
			service.updateCommentNum(vo.getArticleId());
			return new DemoVO("200", JsonUtils.toJSON(vo));
		} catch (Exception e) {
			e.printStackTrace();
			return new DemoVO("500", "评论失败");
		}
	}
	
	@RequestMapping("addToCommentCopy")
	@ResponseBody
	public DemoVO addToCommentCopy(Comment2Vo vo){
		try {
			if(vo!=null){
				String content=vo.getContent();
				vo.setContent(EscapeUtil.escapeHtmlOFLtAndGt(content));			
			}
			vo.setCreateTime(DateUtil.getToDay());
			service.saveCommentCopy(vo);
			return new DemoVO("200", JsonUtils.toJSON(vo));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new DemoVO("500", "评论失败");
		}
	}
	
}
